package shopping;

public class ShoppingCart {
    private int capacity;
    private Product[] products=new Product[10];
    private int i;
    public ShoppingCart() {
    }

    public ShoppingCart(int capacity) {
        this.capacity = capacity;

    }

    public int getI() {
        return capacity;
    }

    public void setI(int i) {
        this.i = i;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public Product[] getProducts() {
        return products;
    }

    public void setProducts(Product[] products) {
        this.products = products;
    }


    public void addProduct(Product product,int quantity) {
        int len=products.length;
        //购物车容量判断（确定已添加物品总数）
        int total=0;
        //确定该商品是购物车中第几个商品
        int count=0;
        for(setI(0);getI()<len;setI(getI()+1)) {
            
            if(products[getI()]!=null) {
                count++;
                products[getI()].setQuantity(quantity);
                total+=products[getI()].getQuantity();
            }
            else if(products[getI()]==null) {
                break;
            }
        }
        if(count==len) {
            System.out.println("购物车可添加商品已达上限");
        }
        else if(total+quantity>capacity) {
            System.out.println("添加物品数量已超出购物车容量");
        }
        else {

            products[i].setQuantity(product.getQuantity());
            products[i].setName(product.getName());
            products[i].setPrice(product.getPrice());
            products[i].setCategory(product.getCategory());
            //this.products[count] = product;
        }
    }

    public void check() {
        for (setI(0); getI() < products.length; setI(getI() + 1)) {
                System.out.println("商品的名称为" + products[getI()].getName());
                System.out.println("商品的价格为" + products[getI()].getQuantity());

        }
    }

    public void deleteProduct(String productName) {
          Product product1=null;
          int count=-1;   //即将删除商品的索引
        for (int i = 0; i < products.length; i++) {
            if (products[i].getName().equals(productName)) {
                product1 = products[i];
                count=i;
                break;
            }
            else{
                System.out.println("该商品信息不存在");
            }
        }
        for(int i=0;i<products.length;i++) {//删除该商品
            if(i>=count&&i!=products.length-1) {
                products[i]=products[i+1];

            }
            else if(i==products.length-1&&products[i]!=null) {
                products[i]=null;
            }
            else{
                products[i]=products[i];
            }
        }
    }


    public void closeAccount() {
        double price_ = 0;
        for (int i = 0; i < products.length; i++) {
            if (products[i] != null) {
                price_ += products[i].getPrice() * products[i].getQuantity();
            }
            else if(products[i]==null&&i==0) {
                price_=0;
            }

        }
        System.out.println("商品的总价为"+price_);
    }

}
